package com.atguigu.gmall.consumer;

import com.atguigu.gmall.constant.MqConst;
import com.atguigu.gmall.service.SearchService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class EsConsumer {

    @Autowired
    private SearchService searchService;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.ON_OFF_SALE_EXCHANGE,durable = "false"),
            key = {MqConst.ON_SALE_ROUTING_KEY},
            value = @Queue(value = MqConst.ON_SALE_QUEUE,durable = "false")
    ))
    public void onSale(Long skuId, Message message, Channel channel) throws IOException {
        searchService.onSale(skuId);//商品上架
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
}
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.ON_OFF_SALE_EXCHANGE,durable = "false"),
            key = {MqConst.OFF_SALE_ROUTING_KEY},
            value = @Queue(value = MqConst.OFF_SALE_QUEUE,durable = "false")
    ))
    public void offSale(Long skuId, Message message, Channel channel) throws IOException {
        searchService.offSale(skuId);//商品下架
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
